package com.un.ebs.core.security;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.access.ExceptionTranslationFilter;
import org.springframework.web.cors.CorsUtils;

/**
 * @email 496382223@qq.com
 * @author:Lijj
 * @date: 2019/7/24
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers().frameOptions().disable();
        http.securityContext();
        // 关闭csrf验证
        http.csrf().disable()
                .authorizeRequests()
                .requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
                .antMatchers("/").permitAll()
                .antMatchers("/ui/**").permitAll()
                .antMatchers("/index.html").permitAll()
                .antMatchers("/login.html").permitAll()
                .antMatchers("/favicon.ico").permitAll()
                .antMatchers("/css/**").permitAll()
                .antMatchers("/js/**").permitAll()
                .antMatchers("/fonts/**").permitAll()
                .antMatchers("/swagger-ui.html").permitAll()
                .antMatchers("/swagger-ui/index.html").permitAll()
                .antMatchers("/swagger-ui").permitAll()
                .antMatchers("/v3/api-docs").permitAll()
                .antMatchers("/druid/**").permitAll()
                .antMatchers("/user/login").permitAll()
                .antMatchers("/user/bindOpenId").permitAll()
                .antMatchers("/user/bindStatus").permitAll()
                .antMatchers("/user/sendRegisterCode").permitAll()
                .antMatchers("/user/checkRegisterCode").permitAll()
                .antMatchers("/personnel/register").permitAll()
                .antMatchers("/order/wxPayNotify/**").permitAll()
                .antMatchers("/order/detail/**").permitAll()
                .antMatchers("/order/payment/wx/**").permitAll()
                .antMatchers("/order/Test").permitAll()
                .antMatchers("/customer/queryBind").permitAll()
                .antMatchers("/dictionary/area/**").permitAll()
                .antMatchers("/context/**").permitAll()
                .antMatchers("/rel/**").permitAll()
                .antMatchers("/common/**").permitAll()
                .antMatchers("/wx/**").permitAll()
//                .antMatchers("/inventoryNote/print/**").permitAll()
                .antMatchers("/productSku/print/**").permitAll()
                .antMatchers("/productionOrder/printBomUser").permitAll()
                .antMatchers("/productionOrder/printWorkOrder").permitAll()
                .antMatchers("/productionOrder/printWorkOrderOut").permitAll()
//                .antMatchers("/saleDeliverNote/print/**").permitAll()
                //.antMatchers("/memorial/**").permitAll()
                //.antMatchers("/memorial/create").permitAll()
                .antMatchers("/product/detail/**").permitAll()
                .antMatchers("/productSku/detail/**").permitAll()
                .antMatchers("/productSku/qrcode/**").permitAll()
                .antMatchers("/productImage/view/**").permitAll()
                //.antMatchers("/productSku/**").permitAll()
                .antMatchers("/recharge-order/test").permitAll()
                .antMatchers("/test/**").permitAll()
                .antMatchers("/weixin/**").permitAll()
                .antMatchers("/v2/api-docs/**",
                        "/configuration/ui/**",
                        "/swagger-resources/**",
                        "/configuration/security/**",
                        "/swagger-ui.html",
                        "/doc.html",
                        "/images/**",
                        "/webjars/**").permitAll()
                .anyRequest().authenticated().and()
                .addFilter(new JwtAuthenticationFilter(authenticationManager()))
                .addFilterAfter(new RewriteAccessDenyFilter(), ExceptionTranslationFilter.class);

    }
}
